﻿using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 将公式Xml文本置入操作系统剪贴板。
        /// </summary>
        /// <param name="onlyDelete">此参数为真时，直接将公式删除，
        /// 而不会将公式文本置入操作系统剪贴板。</param>
        public static void CutOrDelete(bool onlyDelete = false)
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.LeafPanel)
            {
                Command.CutOrDeleteLeaf(onlyDelete); return;
            }

            if (curEx is Expressions.SubPanel)
            {
                MessageBox.Show("子面板不能被“删除”或“剪切”、“复制”、“粘贴”，仅能被复制成图像！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;//子面板不支持Cut。
            }

            if (curEx is Expressions.BaseText)
            {
                Expressions.BaseText curBt = curEx as Expressions.BaseText;
                if (curBt.IsCollapsed)
                {
                    MessageBox.Show("折叠的标题文本块不允许被剪切或删除。此外“复制、粘贴、平移、拖动、更改缩进、对齐、嵌入”等操作亦被禁止。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }

            if (curEx.Parent is StackPanel)
            {
                StackPanel spanel = curEx.Parent as StackPanel;
                int index = spanel.Children.IndexOf(curEx);

                Editor editor = curEx.Editor;

                ModifingItem mi = new ModifingItem();

                if (spanel.Children.Count <= 1)
                {
                    curEx.XmlData.InsertXml(LunarPresentation.Properties.Resources.BaseText);
                    LunarPresentation.Expressions.BaseText newExpression =
                    new LunarPresentation.Expressions.BaseText(curEx.RootPanel, curEx.ParentPanel, curEx.Editor);
                    spanel.Children.Insert(spanel.Children.IndexOf(curEx) + 1, newExpression);
                    newExpression.XmlData = curEx.XmlData.NextSibling;

                    //当前子面板中只有一个公式，且此公式要删除时，
                    //具体操作是：先插入（并选中）一个文本块，然后再删除目标公式
                    //——这样可以保证子面板中至少有一个公式！

                    Action preAction = new Action(newExpression.ExID, spanel.Children.IndexOf(newExpression),
                        typeof(Expressions.BaseText).Name, "InsertExpression", null, newExpression.XmlData.OuterXml,
                        ActionType.Insert, curEx.ParentPanel.ExID);
                    mi.Add(preAction);

                    editor.SelectedExpression = newExpression;
                }

                if (onlyDelete == false)
                {
                    Clipboard.SetData(DataFormats.UnicodeText, curEx.XmlData.OuterXml);
                }

                //准备“撤销”数据。
                Action action = new Action(curEx.ExID, spanel.Children.IndexOf(curEx), curEx.GetType().Name, "DeleteExpression",
                    curEx.XmlData.OuterXml, null, ActionType.Delete, curEx.ParentPanel.ExID);
                mi.Add(action);

                curEx.Editor.Do(mi);

                curEx.XmlData.ParentNode.RemoveChild(curEx.XmlData);
                spanel.Children.RemoveAt(index);

                if (curEx is Expressions.BaseText)
                {
                    Expressions.BaseText bt = curEx as Expressions.BaseText;
                    if (bt.IsHeaderText)
                    {
                        bt.ParentPanel.RefreshAutoNumbersOfBaseText();
                    }
                }

                editor.IsModified = true;

                if (index < spanel.Children.Count)
                {
                    editor.SelectedExpression = spanel.Children[index] as Expressions.Expression;
                }
                else
                {
                    editor.SelectedExpression = spanel.Children[spanel.Children.Count - 1] as Expressions.Expression;
                }

                //改写了Find()方法。
                //curEx.Editor.ParentWindow.spFindResult.Children.Clear();//删除任意一个公式都会清除搜索结果。
            }
        }
    }
}
